
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.抽象工厂模式 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="laiq">
        
        
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-terminal/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../../../styles/website.css">
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3.构造模式.html" />
    
    
    <link rel="prev" href="1.单例模式.html" />
    

    
        <link rel="shortcut icon" href='../../../assets/images/favicon.ico' type="image/x-icon">
    
    
        <link rel="bookmark" href='../../../assets/images/favicon.ico' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../../../assets/images/wukong.png'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../../../assets/images/wukong.png">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../../../assets/images/wukong.png">
        
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../../node/HTTP作用域、上下文.html">
            
                <a href="../../node/HTTP作用域、上下文.html">
            
                    
                    Node 学习
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../../node/HTTP作用域、上下文.html">
            
                <a href="../../node/HTTP作用域、上下文.html">
            
                    
                    HTTP 作用域、上下文
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../../node/Event事件.html">
            
                <a href="../../node/Event事件.html">
            
                    
                    Event 事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../../node/Request.html">
            
                <a href="../../node/Request.html">
            
                    
                    Request
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../../node/爬虫.html">
            
                <a href="../../node/爬虫.html">
            
                    
                    爬虫
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../../node/HTTP跨域.html">
            
                <a href="../../node/HTTP跨域.html">
            
                    
                    HTTP 跨域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../../node/HTML5新特性.html">
            
                <a href="../../node/HTML5新特性.html">
            
                    
                    HTML5 新特性
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="./">
            
                <a href="./">
            
                    
                    PHP 学习
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="./">
            
                <a href="./">
            
                    
                    设计模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1.1" data-path="1.单例模式.html">
            
                <a href="1.单例模式.html">
            
                    
                    1.单例模式
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.1.2" data-path="2.抽象工厂模式.html">
            
                <a href="2.抽象工厂模式.html">
            
                    
                    2.抽象工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.3" data-path="3.构造模式.html">
            
                <a href="3.构造模式.html">
            
                    
                    3.构造模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.4" data-path="4.原型模式.html">
            
                <a href="4.原型模式.html">
            
                    
                    4.原型模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.1.5" data-path="5.工厂方法模式.md">
            
                <span>
            
                    
                    5.工厂方法模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../随手笔记/">
            
                <a href="../随手笔记/">
            
                    
                    随手笔记
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.2.1" data-path="../随手笔记/PHP页面静态化.html">
            
                <a href="../随手笔记/PHP页面静态化.html">
            
                    
                    PHP页面静态化
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../../interview/">
            
                <a href="../../interview/">
            
                    
                    面试相关
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >2.抽象工厂模式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x62BD;&#x8C61;&#x5DE5;&#x5382;abstract-factory">&#x62BD;&#x8C61;&#x5DE5;&#x5382;(Abstract Factory)</h1>
<ul>
<li>&#x4F7F;&#x7528;&#x548C;&#x5B9E;&#x73B0;&#x5206;&#x79BB;</li>
<li>&#x8C03;&#x7528;&#x65B9;&#x53EA;&#x4F9D;&#x8D56;&#x4E8E;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x63A5;&#x53E3;&#xFF0C;&#x4E0E;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x65E0;&#x5173;</li>
</ul>
<h3 id="&#x4EE3;&#x7801;&#xFF1A;">&#x4EE3;&#x7801;&#xFF1A;</h3>
<pre><code class="lang-php"><span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">UserRepository</span></span>{
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findOne</span><span class="hljs-params">($id)</span></span>;
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findAll</span><span class="hljs-params">()</span></span>;
}
<span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">RepositoryFactory</span></span>{
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createUserRepository</span><span class="hljs-params">()</span></span>;
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span></span>{
    <span class="hljs-keyword">private</span> $repositoryFactory;

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">(RepositoryFactory $factory)</span></span>{
        $this-&gt;repositoryFactory = $factory;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">doSomething</span><span class="hljs-params">()</span></span>{
        $userRepository = $this-&gt;repositoryFactory-&gt;createUserRepository();
        $oneUser = $userRepository-&gt;findOne(<span class="hljs-number">1</span>);
    }
}

<span class="hljs-comment">#&#x4E1A;&#x52A1;&#x5B9E;&#x73B0; (MySQL)</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MySqlRepository</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">RepositoryFactory</span></span>{
    <span class="hljs-keyword">private</span> $pdo;

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">()</span></span>{
        $this-&gt;pdo = <span class="hljs-keyword">new</span> PDO();
    }

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createUserRepository</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> MySqlUserRepository($this-&gt;pdo);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MySqlUserRepository</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">UserRepository</span></span>{
    <span class="hljs-keyword">private</span> $pdo;

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">(PDO $pdo)</span></span>{
        $this-&gt;pdo = $pdo;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findOne</span><span class="hljs-params">($id)</span></span>{
        $sql = <span class="hljs-string">&apos;SELECT * FROM t_user where uid = ?&apos;</span>;

        <span class="hljs-keyword">return</span> $result;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findAll</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">return</span> $result;
    }
}

<span class="hljs-comment">#&#x4E1A;&#x52A1;&#x5B9E;&#x73B0;(Redis)</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RedisRepositoryFactory</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">UserRepository</span></span>{
    <span class="hljs-keyword">private</span> $redis;

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">(Redis $redis)</span></span>{
        $this-&gt;redis = $redis;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findOne</span><span class="hljs-params">($id)</span></span>{
        $info = $this-&gt;redis-&gt;GET(<span class="hljs-string">&quot;U:{id}:Info&quot;</span>);
        <span class="hljs-keyword">return</span> json_encode($info);
    }

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findAll</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">return</span> $result;
    }
}
</code></pre>
<h3 id="&#x9002;&#x7528;&#x573A;&#x666F;">&#x9002;&#x7528;&#x573A;&#x666F;:</h3>
<ul>
<li>&#x5265;&#x79BB;&#x8C03;&#x7528;&#x8005;&#x548C;&#x5B9E;&#x73B0;&#x7684;&#x8026;&#x5408;&#xFF0C;&#x5E94;&#x5BF9;&#x53D8;&#x5316;</li>
<li>&#x8C03;&#x7528;&#x8005;&#x3001;&#x5B9E;&#x73B0;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x5206;&#x522B;&#x72EC;&#x7ACB;&#x6D4B;&#x8BD5;</li>
<li>&#x5178;&#x578B;&#x6848;&#x4F8B;:JDBC API &amp; Driver</li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="1.单例模式.html" class="navigation navigation-prev " aria-label="Previous page: 1.单例模式">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3.构造模式.html" class="navigation navigation-next " aria-label="Next page: 3.构造模式">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.抽象工厂模式","level":"1.3.1.2","depth":3,"next":{"title":"3.构造模式","level":"1.3.1.3","depth":3,"path":"docs/php/设计模式/3.构造模式.md","ref":"docs/php/设计模式/3.构造模式.md","articles":[]},"previous":{"title":"1.单例模式","level":"1.3.1.1","depth":3,"path":"docs/php/设计模式/1.单例模式.md","ref":"docs/php/设计模式/1.单例模式.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","include-codeblock","alerts","sitemap-general","favicon","github","terminal","toggle-chapters"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://gitee.com/tonygeli/"},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sitemap-general":{"prefix":"http://gitbook.zhangjikai.com"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":{"shortcut":"./assets/images/favicon.ico","bookmark":"./assets/images/favicon.ico","appleTouch":"./assets/images/wukong.png","appleTouchMore":{"120x120":"./assets/images/wukong.png","180x180":"./assets/images/wukong.png"}},"alerts":{},"include-codeblock":{"check":false,"edit":false,"fixlang":false,"lang":"","template":"ace","theme":"monokai","unindent":true},"terminal":{"copyButtons":true,"fade":false,"style":"flat"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"toggle-chapters":{}},"theme":"default","author":"laiq","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"language":"zh-hans","gitbook":"*","description":"学习记录与分享"},"file":{"path":"docs/php/设计模式/2.抽象工厂模式.md","mtime":"2018-10-30T05:27:25.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-11-01T05:28:55.014Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-terminal/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

